#! /usr/bin/env python
# -*- coding: utf-8 -*-

# *************************************************************
#     Filename @  lru_cache.py
#       Author @  Huoty
#  Create date @  2016-04-09 13:54:25
#  Description @  
# *************************************************************

import time
import urllib
import functools32

def timeit(func):
    @functools32.wraps(func)
    def wrapper(*args, **kw):
        start = time.time()
        result = func(*args, **kw)
        end = time.time()
        print "Total time running %s: %s." % (func.__name__, str(end-start))
        return result
    return wrapper
    
@functools32.lru_cache(maxsize=24) 
@timeit
def get_webpage(num): 
    """ 
    获取特定Python模块网络页面 
    """     
    webpage = "http://www.python.org/dev/peps/pep-%04d/".format(num) 
    print webpage
    try: 
        with urllib.request.urlopen(webpage) as request: 
            return request.read() 
    except Exception:
        return None 

# Script starts from here
 
 
if __name__ == '__main__': 
    numt = (8, 290, 308, 320, 8, 218, 320, 279, 289, 320, 9991) 
    for num in numt: 
        page = get_webpage(num) 
        print page
        if page: 
            print("{} module page found".format(num)) 

    for i in range(8):
        print i
        get_webpage("functools")

    get_webpage("time")
    print get_webpage.cache_info()
    get_webpage.cache_clear()
    print get_webpage.cache_info()
    print dir(get_webpage)

